/*
 * Created on Feb 19, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderSearchForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * @author Ravikanth
**/
public class PurchaseOrderSearchCommand extends ChipTrackExtendedAction
{
	/* 
	 * Search Action Command For all the Search Pages
	 */
	public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HashMap errors          = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String action   		= request.getParameter( REQUEST_ACTION );
		String search 			= request.getParameter( ACTION_SEARCH );
		String actionForward	= "search";
		HttpSession mSession 	= request.getSession();
		PurchaseOrderSearchForm purchaseOrderSearchForm = ( PurchaseOrderSearchForm )form;
		
		try
		{
			request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
			request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
            request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );
			if ( ACTION_CHANGE_PURCHASE_ORDER_SEARCH.equalsIgnoreCase( action ))
            {
                // don't load the "Cancelled" status type
                request.setAttribute( "purchaseOrderStatusList", PurchaseOrderDAO.loadPurchaseOrderStatusTypes( false) );
            }
            else
            {
                request.setAttribute( "purchaseOrderStatusList", PurchaseOrderDAO.loadPurchaseOrderStatusTypes( true) );
            }
			request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );

			if( search != null )
			{
				ArrayList purchaseOrderSearchList = PurchaseOrderDAO.searchPurchaseOrder( purchaseOrderSearchForm );

				if( purchaseOrderSearchList != null && purchaseOrderSearchList.size() != 0 )
				{
					purchaseOrderSearchForm.setPurchaseOrderSearchList( purchaseOrderSearchList );
				}
				else
				{
					info.put( INFO_PURCHASE_ORDERS_WITH_SEARCH_CRITERIA_NOT_FOUND, null );
				}
			}

			actionForward = "search";
		}
		catch ( Exception e )
		{
			e.printStackTrace();
			Logger.error( ERROR_LOADING_PURCHASE_ORDER_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_PURCHASE_ORDER_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "search";
		}

        if( search == null )
	    {
            purchaseOrderSearchForm.setVendor_id( -1 );
            purchaseOrderSearchForm.setPoType( -1  );
        }
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}
}
